package com.mingtu.guards.activity;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.guards.R;
import com.mingtu.guards.adapter.TabbarAdapter;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.fragment.Task1Fragment;
import com.mingtu.guards.fragment.Task2Fragment;

import java.util.ArrayList;

import butterknife.BindView;

public class TaskListActivity extends MyBaseActivity {


    @BindView(R.id.tab_nav)
    CommonTabLayout tabNav;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private static final String[] mTitles = {"巡护任务", "其他任务"};
    private static final int[] mSeleIcons = {R.mipmap.icon_task_track_selected, R.mipmap.icon_task_selected};
    private static final int[] mNormalIcons = {R.mipmap.icon_task_track_normal, R.mipmap.icon_task_normal};

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_task_list;
    }

    @Override
    protected void initView() {
        setModuleTitle("任务单");
        setTitleBarBackground(getResources().getDrawable(com.mingtu.center.R.drawable.titlebar_line_bottom));
    }

    @Override
    protected void initData() {

        mFragments.add(new Task1Fragment());
        mFragments.add(new Task2Fragment());

        viewPager.setOffscreenPageLimit(2);//需要设置此值
        viewPager.setAdapter(new TabbarAdapter(getSupportFragmentManager(), mFragments));
        viewPager.addOnPageChangeListener(onPageChangeListener);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mSeleIcons[i], mNormalIcons[i]));
        }

        tabNav.setTabData(mTabEntities);
        tabNav.setOnTabSelectListener(onTabSelectListener);
    }

    @Override
    protected void getData() {

    }


    private OnTabSelectListener onTabSelectListener = new OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            viewPager.setCurrentItem(position, false);
        }

        @Override
        public void onTabReselect(int position) {

        }
    };

    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tabNav.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
}